import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:word_app/app/modules/model/screen_adapter.dart';
import 'package:word_app/app/modules/model/toast_model.dart';

import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  const LoginView({super.key});
  @override
  Widget build(BuildContext context) {
    LoginController controller = Get.put(LoginController());
    return WillPopScope(
        child: Scaffold(
            appBar: AppBar(
              automaticallyImplyLeading: false, //隐藏返回按钮
              title: const Text(
                '登录',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              centerTitle: true,
            ),
            body: Obx(
              () => SingleChildScrollView(
                child: Container(
                  margin: EdgeInsets.only(
                      top: ScreenAdapterModel.height(40),
                      bottom: ScreenAdapterModel.height(100)),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Center(
                        child: Container(
                          margin: EdgeInsets.only(
                              bottom: ScreenAdapterModel.height(100)),
                          width: ScreenAdapterModel.width(80),
                          height: ScreenAdapterModel.width(80),
                          child: Image.asset("images/appLogo.png"),
                        ),
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 40, right: 40),
                        child: TextField(
                          controller: controller.phoneTFVc,
                          decoration: const InputDecoration(
                            labelStyle: TextStyle(color: Colors.black54),
                            hintText: '请输入手机号',
                            hintStyle: TextStyle(color: Colors.black26),
                            focusedBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.black45),
                            ),
                          ),
                        ),
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 40, right: 40),
                        child: Stack(
                          children: [
                            TextField(
                              controller: controller.codeTFVc,
                              decoration: const InputDecoration(
                                labelStyle: TextStyle(color: Colors.black54),
                                hintText: '验证码',
                                hintStyle: TextStyle(color: Colors.black26),
                                focusedBorder: UnderlineInputBorder(
                                  borderSide: BorderSide(color: Colors.black45),
                                ),
                              ),
                            ),
                            Positioned(
                                right: 0,
                                top: 20,
                                child: InkWell(
                                  onTap: () {
                                    if (controller.phoneTFVc.text.length !=
                                        11) {
                                      ToastModel.showToast("请输入正确的手机号码");
                                      return;
                                    }

                                    if (controller.remainingTimer.value < 1) {
                                      controller.remainingTimer.value = 60;
                                    }
                                    if (controller.remainingTimer.value > 0 &&
                                        controller.remainingTimer.value < 60) {
                                      return;
                                    }
                                    controller.startTimer();
                                  },
                                  child: Text(
                                    (controller.remainingTimer.value == 60 ||
                                            controller.remainingTimer.value < 1)
                                        ? "发送验证码"
                                        : "${controller.remainingTimer}s",
                                    style: const TextStyle(
                                        color:
                                            Color.fromARGB(255, 99, 138, 231)),
                                  ),
                                )),
                          ],
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.only(top: 20),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            InkWell(
                              onTap: () {
                                controller.selectProAction();
                              },
                              child: Container(
                                width: ScreenAdapterModel.width(25),
                                height: ScreenAdapterModel.width(25),
                                decoration: BoxDecoration(
                                    color: controller.selectPro.value
                                        ? Colors.blue
                                        : Colors.white,
                                    borderRadius: BorderRadius.circular(
                                        ScreenAdapterModel.width(12)),
                                    border: Border.all(
                                        color: controller.selectPro.value
                                            ? Colors.blue
                                            : const Color.fromARGB(
                                                255, 158, 158, 158),
                                        width: 2)),
                              ),
                            ),
                            const Text(" 我已阅读并同意"),
                            InkWell(
                              onTap: () {},
                              child: const Text(
                                "《用户协议》",
                                style: TextStyle(
                                    color: Color.fromARGB(255, 99, 138, 231)),
                              ),
                            ),
                            InkWell(
                              onTap: () {},
                              child: const Text(
                                "《隐私政策》",
                                style: TextStyle(
                                    color: Color.fromARGB(255, 99, 138, 231)),
                              ),
                            )
                          ],
                        ),
                      ),
                      InkWell(
                        child: Container(
                          margin: const EdgeInsets.only(top: 50),
                          alignment: Alignment.center,
                          height: ScreenAdapterModel.width(60),
                          width: ScreenAdapterModel.width(300),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(
                                  ScreenAdapterModel.width(40)),
                              color: const Color.fromARGB(255, 112, 128, 255)),
                          child: const Text(
                            "立即登录",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontSize: 18),
                          ),
                        ),
                        onTap: () {
                          if (controller.selectPro.value == false) {
                            ToastModel.showToast("请同意协议");
                            return;
                          }
                          if (controller.codeTFVc.text.isEmpty) {
                            ToastModel.showToast("请输入验证码");
                            return;
                          }
                          controller.loginApp();
                        },
                      )
                    ],
                  ),
                ),
              ),
            )),
        onWillPop: () async {
          return false; // 禁止物理返回按钮和右滑返回手势
        });
  }
}
